import os
from datetime import datetime
import step_2_3_clear_resume_breakpoint
import config_setting, util_path, util_store


def start(inbox_folder_path):
    # inbox_folder_path = config_setting.inbox_root_folder_path  # E:/WebArchivesDownload/inbox

    url_done_list_file_path = os.path.join(config_setting.vault_root_folder_path, "done_snapshot_url_list.txt")  # E:/WebArchivesDownload/done_snapshot_url_list.txt

    url_list = []
    for dirpath, filename in util_path.travel_file(inbox_folder_path,
                                                   lambda dirpath, filename: filename.startswith(
                                                           "resume_breakpoint_url_list.txt")):
        url_list += util_store.read_url_list(os.path.join(dirpath, filename))

    url_list = list(set(url_list))
    url_list.insert(0, f"### {datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}")
    util_store.append_url_list(url_done_list_file_path, url_list)

    step_2_3_clear_resume_breakpoint.start(inbox_folder_path)
    print("done")


if __name__ == "__main__":
    """
    将断点续传状态清除，统一刷新至done_snapshto_url_list文件中
    收集vault下所有子目录中的done_snapshot_url_list.txt文件，并把他们汇总去重后保存至特定路径
    一般意义理解统计为一次take snapshot操作总共下载了多了网页（有些可能下载失败，但也统计在里面）
    """
    inbox_folder_path = f"{config_setting.inbox_root_folder_path}/{config_setting.batch_name}"  # ./assets/20240502
    start(inbox_folder_path)
